// Resource
// (c) jimon game studio

#ifndef JEH_IRESOURCE
#define JEH_IRESOURCE

#include "jeUnknown.h"
#include "jeStl.h"

namespace je
{
	namespace media
	{
		//! Resource
		class IResource:public je::jeUnknown
		{
		protected:
			//! Current Resource Name
			jeStringc CurrentResourceName;

			//! Usage Counter
			s32 UsageCounter;

			//! Loaded
			u1 ResourceLoaded;

			//! Need Unload
			u1 NeedUnload;

			//! Parameters Load
			u1 ParametersLoad;

			//! Enable Loading
			u1 EnableLoading;

		public:
			//! Constructor
			IResource(const jeStringc & ResourceName,u1 SetEnableLoading = true)
				:CurrentResourceName(ResourceName),UsageCounter(0),ResourceLoaded(false),NeedUnload(false),ParametersLoad(false),EnableLoading(SetEnableLoading)
			{
			}

			//! Destructor
			virtual ~IResource()
			{
			}

			//! Get Type Name
			virtual jeStringc GetTypeName() = 0;

			//! Get Name
			virtual const jeStringc & GetName()
			{
				return CurrentResourceName;
			}

			//! Load Parameters
			virtual void LoadParameters()
			{
				ParametersLoad = true;
			}

			//! Load Resource
			virtual void Load() = 0;

			//! UnLoad Resource
			virtual void UnLoad() = 0;

			//! Add Usage
			virtual void AddUsage()
			{
				UsageCounter++;

				if(!ResourceLoaded)
				{
					if(EnableLoading)
						Load();
					ResourceLoaded = true;
				}

				if(NeedUnload)
					NeedUnload = false;
			}

			//! Remove Usage
			virtual void RemoveUsage()
			{
				UsageCounter--;

				if((!UsageCounter) && ResourceLoaded)
				{
					NeedUnload = true;
				}
			}

			//! Is need to unload
			virtual u1 IsNeedToUnload()
			{
				return NeedUnload;
			}

			//! Garbage Unload
			virtual void GarbageUnload()
			{
				if(EnableLoading)
					UnLoad();
				ResourceLoaded = false;
				NeedUnload = false;
			}
		};
	}
}

#define JE_RESOURCEBASE_GET( __Name, __Type ) static_cast<je::media::je##__Type *>(je::media::GetResourceManager()->GetResource(#__Type, __Name ))



#endif
